package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.application.AppQuestData;
import com.bangdream.michelia.entity.QuestAnswerBean;
import com.bangdream.michelia.entity.exam.AnswerBean;
import com.bangdream.michelia.entity.exam.ExamQuestBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @date on 2018/6/18 15:04
 * @email 909732184@qq.com
 * 问卷调差的可选答案 网格布局适配器
 * 单选题适配器
 */
public class QuestAnswerSelectAdapter extends RecyclerView.Adapter {
    private List<ExamQuestBean.AnswerInfoListBean> data = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;
    private int nmPostion=0;
    private OnViewItemClickListener onViewItemClickListener;
    private int position ;
    private String infoId,id;
    public OnViewItemClickListener getOnViewItemClickListener() {
        return onViewItemClickListener;
    }

    public void setOnViewItemClickListener(OnViewItemClickListener onViewItemClickListener) {
        this.onViewItemClickListener = onViewItemClickListener;
    }

    /*public List<ExamQuestBean.AnswerInfoListBean> getData() {
        return data;
    }

    public void setData(List<ExamQuestBean.AnswerInfoListBean> data) {
        this.data = data;
    }*/
    public void notifyDataChange( List<ExamQuestBean.AnswerInfoListBean> data,String infoId,String id,int position){
        this.infoId = infoId;
        this.id = id;
        this.data  = data;
        this.position = position;
        notifyDataSetChanged();
    }
    public interface OnViewItemClickListener{
        void onCurriculumItemClick(int position);
    }

    public int getNmPostion() {
        return nmPostion;
    }

    public void setNmPostion(int nmPostion) {
        this.nmPostion = nmPostion;
    }

    public QuestAnswerSelectAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.answer_gridview_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        if(holder instanceof ViewHolder){
            ((ViewHolder) holder).tvOption.setText(data.get(position).getNo());
            final List<QuestAnswerBean> list = AppQuestData.getInstance().getQuestAnswerList();
            final ExamQuestBean.AnswerInfoListBean infoListBean = data.get(position);
            viewHolder.tvOption.setText(infoListBean.getDescription());
            viewHolder.tvOption.setTag(position);
            viewHolder.tvOption.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(infoListBean.isCheck()){
                        viewHolder.ivOption.setBackgroundResource(R.mipmap.kaoshi_bj_answer_a);
                        infoListBean.setCheck(false);
                        if(list.size()>0){
                            list.remove(position);
                        }
                    }else{
                        viewHolder.ivOption.setBackgroundResource(R.mipmap.kaoshi_bj_answer_b);
                        infoListBean.setCheck(true);
                        QuestAnswerBean bean = new QuestAnswerBean();
                        bean.setId(id);
                        bean.setAnswer(infoListBean.getNo());
                        bean.setInfoId(infoId);
                        bean.setPosition(position);
                        bean.setQuestionId(infoListBean.getQuestionId());
                        list.add(bean);
                    }
                }
            });
            viewHolder.ivOption.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(infoListBean.isCheck()){
                        viewHolder.ivOption.setBackgroundResource(R.mipmap.kaoshi_bj_answer_a);
                        infoListBean.setCheck(false);
                        if(list.size()>0){
                            list.remove(position);
                        }
                    }else{
                        viewHolder.ivOption.setBackgroundResource(R.mipmap.kaoshi_bj_answer_b);
                        infoListBean.setCheck(true);
                        /*AnswerBean bean = new AnswerBean();
                        bean.setId(infoListBean.getId());
                        bean.setAnswer(infoListBean.getNo());
                        bean.setQuestionId(infoListBean.getQuestionId());*/
                        QuestAnswerBean bean = new QuestAnswerBean();
                        bean.setId(id);
                        bean.setAnswer(infoListBean.getNo());
                        bean.setInfoId(infoId);
                        bean.setPosition(position);
                        bean.setQuestionId(infoListBean.getQuestionId());
                        list.add(bean);
                    }
                }
            });
        }
    }
    protected  List<ExamQuestBean.AnswerInfoListBean> getCurData(){
        return data;
    }


    @Override
    public int getItemCount() {
        return nmPostion;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvOption;
        private ImageView ivOption;

        public ViewHolder(View itemView) {
            super(itemView);
            tvOption = itemView.findViewById(R.id.tv_option);
            ivOption = itemView.findViewById(R.id.iv_option);
        }
    }
}
